/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.reportes;

import com.camachosanchez.bean.Vehiculos;
import com.camachosanchez.util.ReportUtil;
import com.camachosanchez.util.SQLUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Desktop;
import java.awt.Point;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;

/**
 *
 * @author Zully
 */
public class ImprimirUnidad {
    
    public void generarReporte(Vehiculos vehiculo) {
        Document document = new Document(PageSize.A4, 0, 0, 50, 50);
        try {
            SimpleDateFormat formatoDeFecha = new SimpleDateFormat("yyyy-MM-dd");
            
            PdfWriter.getInstance(document, new FileOutputStream("reportes/unidad_" + vehiculo.getPlaca() + ".pdf"));
            document.open();
            document.addTitle("Unidad: " + vehiculo.getPlaca());
            
            float[] anchoColsT = {1.1f, 0.6f};//version prueba
            PdfPTable tablaTitulo=new PdfPTable(anchoColsT);
            tablaTitulo.setSpacingAfter(20f);
            tablaTitulo.setHorizontalAlignment(Element.ALIGN_CENTER);
            tablaTitulo.setWidthPercentage(90);

            PdfPCell celda =new PdfPCell (new Paragraph("Unidad " + vehiculo.getPlaca(),
            FontFactory.getFont(BaseFont.HELVETICA,18,Font.BOLD,BaseColor.BLACK)));
            celda.setHorizontalAlignment(Element.ALIGN_CENTER);
            celda.setBorderWidth(0);
            tablaTitulo.addCell(celda);

            tablaTitulo.addCell(ReportUtil.getCelda("TireSoft 2.0 (Versión de prueba)",1,1,BaseColor.WHITE,0,0,10,1));//version prueba
            document.add(tablaTitulo);
            
            float[] anchoColsV = {1.7f, 1.8f, 1.5f, 2f, 1.5f, 2f, 1.5f, 2f};
            PdfPTable tablaV=new PdfPTable(anchoColsV);
            tablaV.setWidthPercentage(90);
            tablaV.addCell(ReportUtil.getCelda("Información Principal",8,1,new BaseColor(217,227,255),0,0.5f,10,1));
            tablaV.addCell(ReportUtil.getCelda("Número de placa:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getPlaca(),2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Serie de chasis:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getSerieChasis(),2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Número de motor:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getNumeroMotor(),2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Serie de factura:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getNumFacturaCompra(),2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Código de barras:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getCodBarras(),2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Fecha de registro:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(formatoDeFecha.format(vehiculo.getFechaRegistro()),2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            
            String valor;
            tablaV.addCell(ReportUtil.getCelda(" ",8,1,new BaseColor(255,255,255),0,0.5f,10,1));
            tablaV.addCell(ReportUtil.getCelda("Información Técnica",4,1,new BaseColor(217,227,255),0,0.5f,10,1));
            tablaV.addCell(ReportUtil.getCelda("Información de Almacenamiento",4,1,new BaseColor(217,227,255),0,0.5f,10,1));
            tablaV.addCell(ReportUtil.getCelda("Tipo de unidad:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            valor = (vehiculo.getIdTipo()==null)? "" : vehiculo.getIdTipo().getNombre();
            tablaV.addCell(ReportUtil.getCelda(valor,2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Número de inventario:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getNumeroInventario(),2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Marca de unidad:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            valor = (vehiculo.getIdMarca()==null)? "" : vehiculo.getIdMarca().getNombre();
            tablaV.addCell(ReportUtil.getCelda(valor,2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Fecha de compra:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(formatoDeFecha.format(vehiculo.getFechaCompra()),2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Tipo de combustible:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getIdCombustible().getNombre(),2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Razón social:",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            valor = (vehiculo.getIdFlotilla()==null)? "" : vehiculo.getIdFlotilla().getNombre();
            tablaV.addCell(ReportUtil.getCelda(valor,2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Capacidad del tanque (L):",2,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getCapacidadTanque(),2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Carga (kg):",1,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getMaximaCapacidadCargaPeso() + "",1,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Carga (m³):",1,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getMaximaCapacidadCargaVolumen() + "",1,1,new BaseColor(255,255,255),0,0.5f,9,0));
            
            tablaV.addCell(ReportUtil.getCelda(" ",8,1,new BaseColor(255,255,255),0,0.5f,10,1));
            tablaV.addCell(ReportUtil.getCelda("Sistema de Ejes",8,1,new BaseColor(217,227,255),0,0.5f,10,1));
            tablaV.addCell(ReportUtil.getCelda("Nombre del modelo:",1,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getIdTipoEje().getNombre() + "",2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda(" ",5,3,new BaseColor(255,255,255),0,0.5f,9,1));
            Point p = SQLUtil.numeroEjes(vehiculo.getIdTipoEje().getId());
            tablaV.addCell(ReportUtil.getCelda("Número de ejes:",1,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(p.x + "",2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Número de llantas:",1,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(p.y + "",2,1,new BaseColor(255,255,255),0,0.5f,9,0));
            
            tablaV.addCell(ReportUtil.getCelda("Distribuidor, Costos y Comentarios",8,1,new BaseColor(217,227,255),0,0.5f,10,1));
            tablaV.addCell(ReportUtil.getCelda("Distribuidor:",1,1,new BaseColor(255,255,255),0,0.5f,9,1));
            valor = (vehiculo.getIdDistribuidor()==null)? "" : vehiculo.getIdDistribuidor().getNombre();
            tablaV.addCell(ReportUtil.getCelda(valor,3,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Forma de pago:",1,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getIdFormaPago().getNombre(),1,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Precio ($):",1,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda(vehiculo.getPrecioVehiculo() + "",1,1,new BaseColor(255,255,255),0,0.5f,9,0));
            tablaV.addCell(ReportUtil.getCelda("Comentarios:",1,1,new BaseColor(255,255,255),0,0.5f,9,1));
            tablaV.addCell(ReportUtil.getCelda("",7,1,new BaseColor(255,255,255),0,0.5f,9,0));
            
            document.add(tablaV);
            
            document.close();
            
            File pdfFile = new File("reportes/unidad_" + vehiculo.getPlaca() + ".pdf");
            if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().open(pdfFile);
            } else {
                JOptionPane.showMessageDialog(null, "No se puede obtener la vista previa.", "Error al abrir el archivo", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    }
}
